package com.yanqu.road.server.cmd.activity.unionwar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eItemId;
import com.yanqu.road.pb.activity.unionwar.UnionWarActivityServerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.unionwar.UnionPhaseWarMgr;
import com.yanqu.road.server.manger.activity.unionwar.UnionWarActivityMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;


@Cmd(code = Protocol.S_UNION_WAR_USER_GOODS_USER,desc = "备战-锦囊使用")
public class UnionWarUserUseGoodsCmd extends AbstractCommand {

    private int activityId;
    private int phase;
    private int session;
    private int goodsId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        UnionWarActivityServerProto.UserDispatchReqMsg reqMsg = UnionWarActivityServerProto.UserDispatchReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        phase = reqMsg.getPhase();
        session = reqMsg.getSession();
        goodsId = reqMsg.getGoodsId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        //if(goodsId == 0){
        //    ret = GameErrorCode.E_UNION_WAR_ACTIVITY_NO_APPOINT_GOODS;
        //    return;
        //}
        //goodsId 0代表取消
        if(goodsId != 0 && goodsId != eItemId.UNION_WAR_ACTIVITY_BSYZ_JN.getValue()  && goodsId != eItemId.UNION_WAR_ACTIVITY_NYSB_JN.getValue()
                && goodsId != eItemId.UNION_WAR_ACTIVITY_SRPZ_JN.getValue() && goodsId != eItemId.UNION_WAR_ACTIVITY_SMFB_JN.getValue()){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }
        ActivityInfo activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
        if(activityInfo == null){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        String unionUid = GameUnionActivityGroupMgr.getUnionUid(activityId, player.getUserId());
        if(StringUtils.isNullOrEmpty(unionUid)){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }
        ret = UnionPhaseWarMgr.dispatchUserGoods(player, activityId, unionUid, phase, session, goodsId);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            UnionWarActivityServerProto.UserDispatchRespMsg.Builder respMsg = UnionWarActivityServerProto.UserDispatchRespMsg.newBuilder();
            respMsg.setRet(ret);
            respMsg.setPhase(phase);
            respMsg.setSession(session);
            player.sendPacket(Protocol.U_UNION_WAR_USER_PATRONS_DISPATCH, respMsg);
        }
    }
}
